<?php


class EmsSectionTable extends Doctrine_Table
{
    public function addSectionsByTitleLikeQuery($string, $limit, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsSection s');
        }

        $alias = $q->getRootAlias();

        $q->andWhere('LOWER('.$alias.'.title) LIKE LOWER(?)', '%'.$string.'%')
                ->addOrderBy($alias.'.title')->limit($limit);

        return $q;
    }

    public function addEnabledSectionsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsSection s');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.is_enabled = ?', 1);

        return $q;
    }

    public function addPublishedSectionsQuery(Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsSection s');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.published = ?', 1);

        return $q;
    }

    public function addPortalSectionsQuery($portal_id, Doctrine_Query $q = null)
    {
        if (is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsSection s');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.portal_id = ?', $portal_id);

        return $q;
    }

    public function getEnabledAndPublishedPortalSectionsByTitleLike($string, $limit, $portal_id)
    {
        return $this->addSectionsByTitleLikeQuery($string, $limit,
                    $this->addEnabledSectionsQuery(
                        $this->addPublishedSectionsQuery(
                            $this->addPortalSectionsQuery($portal_id))))
                ->execute();
    }

    public static function getInstance()
    {
        return Doctrine_Core::getTable('EmsSection');
    }
}